package com.ourteam.workspace.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.log4j.Logger;

import com.ourteam.system.domain.SystemUser;
import com.ourteam.workspace.dao.IWorkspaceResourceDAO;
import com.ourteam.workspace.dao.ProjectServerRelate;
import com.ourteam.workspace.dao.ProjectWorkspaceRelate;
import com.ourteam.workspace.dao.ProjectWorkspaceRelateDAOQueryBean;
import com.ourteam.workspace.dao.Workspace;
import com.ourteam.workspace.dao.WorkspaceBuildConfig;
import com.ourteam.workspace.dao.WorkspaceBuildConfigDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceBuildLog;
import com.ourteam.workspace.dao.WorkspaceBuildLogDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceCompiler;
import com.ourteam.workspace.dao.WorkspaceCompilerDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceConfig;
import com.ourteam.workspace.dao.WorkspaceConfigDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceConfigProperty;
import com.ourteam.workspace.dao.WorkspaceConfigPropertyDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceFileTypeConfig;
import com.ourteam.workspace.dao.WorkspaceFileTypeConfigDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceMember;
import com.ourteam.workspace.dao.WorkspaceMemberDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelateDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseRelate;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseRelateDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceResource;
import com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelate;
import com.ourteam.workspace.dao.WorkspaceResourceBusinessPackageRelateDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceResourceDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceResourceTypeEnum;
import com.ourteam.workspace.dao.WorkspaceResourceUIPageRelate;
import com.ourteam.workspace.dao.WorkspaceResourceUIPageRelateDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceScmChangePathLogDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceScmConfig;
import com.ourteam.workspace.dao.WorkspaceScmConfigDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceScmLogDAOQueryBean;
import com.ourteam.workspace.dao.WorkspaceServer;
import com.ourteam.workspace.dao.WorkspaceServerDAOQueryBean;
import com.ourteam.workspace.domain.WorkspaceBean;
import com.ourteam.workspace.domain.WorkspaceBuildConfigBean;
import com.ourteam.workspace.domain.WorkspaceBuildLogBean;
import com.ourteam.workspace.domain.WorkspaceCompilerBean;
import com.ourteam.workspace.domain.WorkspaceConfigBean;
import com.ourteam.workspace.domain.WorkspaceConfigPropertyBean;
import com.ourteam.workspace.domain.WorkspaceFileTypeConfigBean;
import com.ourteam.workspace.domain.WorkspaceMemberBean;
import com.ourteam.workspace.domain.WorkspaceProductDatabaseModelRelateBean;
import com.ourteam.workspace.domain.WorkspaceProductDatabaseRelateBean;
import com.ourteam.workspace.domain.WorkspaceResourceBean;
import com.ourteam.workspace.domain.WorkspaceResourceBusinessPackageRelateBean;
import com.ourteam.workspace.domain.WorkspaceResourceUIPageRelateBean;
import com.ourteam.workspace.domain.WorkspaceScmChangePathLogBean;
import com.ourteam.workspace.domain.WorkspaceScmConfigBean;
import com.ourteam.workspace.domain.WorkspaceScmLogBean;
import com.ourteam.workspace.domain.WorkspaceServerBean;

public class WorkspaceServiceImpl extends AbstractWorkspaceService {

	/**
	 * F Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(WorkspaceServiceImpl.class);

	private static final String WORKSPACE_ROOT_PATH = "/WorkspaceRoot/";

	@Override
	protected WorkspaceBean[] doGetWorkspacesByProjectIds(long[] projectIds) throws Exception {

		if (ArrayUtils.isEmpty(projectIds)) {
			return new WorkspaceBean[0];
		}

		ProjectWorkspaceRelateDAOQueryBean queryBean = new ProjectWorkspaceRelateDAOQueryBean();

		queryBean.createCriteria().andProjectIdIn(projectIds);

		// ProjectWorkspaceRelate[] relates = this.projectWorkspaceRelateDao
		// .query(queryBean);

		ProjectWorkspaceRelate[] relates = new ProjectWorkspaceRelate[0];

		if (ArrayUtils.isEmpty(relates)) {
			return new WorkspaceBean[0];
		}

		long[] workspaceIds = new long[relates.length];

		for (int i = 0; i < relates.length; i++) {
			workspaceIds[i] = relates[i].getWorkspaceId();
		}

		WorkspaceDAOQueryBean workspaceDAOQueryBean = new WorkspaceDAOQueryBean();

		workspaceDAOQueryBean.createCriteria().andIdIn(workspaceIds);

		return WorkspaceBean.toArray(this.workspaceDao.query(workspaceDAOQueryBean));
	}

	@Override
	protected WorkspaceResourceBean[] doGetWorkspaceResources(long workspaceId,
			WorkspaceResourceTypeEnum[] workspaceResourceTypeEnums) throws Exception {

		String[] resourceTypes;

		if (ArrayUtils.isNotEmpty(workspaceResourceTypeEnums)) {
			resourceTypes = new String[workspaceResourceTypeEnums.length];

			for (int i = 0; i < resourceTypes.length; i++) {
				resourceTypes[i] = workspaceResourceTypeEnums[i].getName();
			}

		} else {
			resourceTypes = ArrayUtils.EMPTY_STRING_ARRAY;
		}

		WorkspaceResourceDAOQueryBean queryBean = new WorkspaceResourceDAOQueryBean();

		WorkspaceResourceDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andWorkspaceIdEqualTo(workspaceId);

		if (ArrayUtils.isNotEmpty(resourceTypes)) {
			criteria.andResourceTypeIn(resourceTypes);
		}

		queryBean.setSortProperty(IWorkspaceResourceDAO.ResourceType);

		queryBean.setSortProperty(IWorkspaceResourceDAO.OrderIndex);

		return WorkspaceResourceBean.toArray(this.workspaceResourceDao.query(queryBean));
	}

	@Override
	protected void doAddNewWorkspace(WorkspaceBean workspaceBean) throws Exception {

		// String workspacePath = workspaceBean.getWorkspacePath();

		// File rootPath = getWorkspaceRoot();
		//
		// File workspaceDir = new File(rootPath, workspacePath);

		// if (workspaceDir.exists() == false) {
		// FileUtils.forceMkdir(workspaceDir);
		// }
		//
		// logger.debug("Workspace Path:" + workspaceDir.getPath());

		workspaceBean.setCreateTime(new Date());

		if (workspaceBean.getSubmitterId() != 0) {
			SystemUser systemUser = this.sysUserService.getSysUserById(workspaceBean.getSubmitterId());
			if (systemUser != null) {
				workspaceBean.setCreatorName(systemUser.getName());
			}
		}

		workspaceDao.insert(workspaceBean.convertToWorkspace());

		// ProjectWorkspaceRelate projectWorkspaceRelate = new
		// ProjectWorkspaceRelate();
		//
		// projectWorkspaceRelate.setProjectId(projectId);
		//
		// projectWorkspaceRelate.setWorkspaceId(workspaceBean.getId());

		// projectWorkspaceRelateDao
		// .insert(projectWorkspaceRelate);

	}

	@Override
	protected void doModifyWorkspace(WorkspaceBean aWorkspaceBean) throws Exception {

		aWorkspaceBean.setLastModifyTime(new Date());

		if (aWorkspaceBean.getSubmitterId() != 0) {
			SystemUser systemUser = this.sysUserService.getSysUserById(aWorkspaceBean.getSubmitterId());
			if (systemUser != null) {
				aWorkspaceBean.setLastModifierName(systemUser.getName());
			}
		}

		workspaceDao.update(aWorkspaceBean.convertToWorkspace());

	}

	protected File getWorkspaceRoot() throws Exception {
		File rootPath = new File(WORKSPACE_ROOT_PATH);

		if (rootPath.exists() == false) {
			rootPath.mkdirs();
		}

		return rootPath;
	}

	@Override
	protected void doAddWorkspaceResource(WorkspaceResourceBean workspaceResourceBean) throws Exception {

		WorkspaceResourceDAOQueryBean queryBean = new WorkspaceResourceDAOQueryBean();

		queryBean.setSortProperty(IWorkspaceResourceDAO.OrderIndex);

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceResourceBean.getWorkspaceId());

		WorkspaceResource[] resources = this.workspaceResourceDao.query(queryBean);

		int index = 1;

		for (int i = 0; i < resources.length; i++) {
			resources[i].setOrderIndex(index++);
		}

		this.workspaceResourceDao.batchUpdate(resources);

		workspaceResourceBean.setOrderIndex(index);

		this.workspaceResourceDao.insert(workspaceResourceBean.convertToWorkspaceResource());
	}

	@Override
	protected WorkspaceBean doGetWorkspaceById(long workspaceId) throws Exception {
		Workspace workspace = this.workspaceDao.findById(workspaceId);
		if (workspace == null) {
			return null;
		} else {
			return new WorkspaceBean(workspace);
		}

	}

	@Override
	protected void doAddWorkspaceResourceBusinessPackageRelate(long resourceId, long[] businessPackageIds)
			throws Exception {
		WorkspaceResourceBusinessPackageRelate[] relates = new WorkspaceResourceBusinessPackageRelate[businessPackageIds.length];

		for (int i = 0; i < relates.length; i++) {
			relates[i] = new WorkspaceResourceBusinessPackageRelate();
			relates[i].setWorkspaceResourceId(resourceId);
			relates[i].setBusinessPcakageId(businessPackageIds[i]);
		}

		WorkspaceResourceBusinessPackageRelateDAOQueryBean queryBean = new WorkspaceResourceBusinessPackageRelateDAOQueryBean();

		queryBean.createCriteria().andWorkspaceResourceIdEqualTo(resourceId).andBusinessPcakageIdIn(businessPackageIds);

		this.workspaceResourceBusinessPackageRelateDao.synchronize(relates, queryBean);
	}

	@Override
	protected void doRemoveWorkspaceResourceBusinessPackageRelates(long[] relateIds) throws Exception {

		WorkspaceResourceBusinessPackageRelateDAOQueryBean queryBean = new WorkspaceResourceBusinessPackageRelateDAOQueryBean();

		queryBean.createCriteria().andIdIn(relateIds);

		this.workspaceResourceBusinessPackageRelateDao.delete(queryBean);

	}

	@Override
	protected WorkspaceResourceBean doGetWorkspaceResourceById(long workspaceResourceId) throws Exception {
		WorkspaceResource workspaceResource = this.workspaceResourceDao.findById(workspaceResourceId);
		return workspaceResource == null ? null : new WorkspaceResourceBean(workspaceResource);
	}

	@Override
	protected void doModifyWorkspaceResource(WorkspaceResourceBean workspaceResourceBean) throws Exception {

		WorkspaceResource workspaceResource = workspaceResourceBean.convertToWorkspaceResource();

		this.workspaceResourceDao.update(workspaceResource);
	}

	@Override
	protected WorkspaceResourceBusinessPackageRelateBean[] doGetWorkspaceResourceBusinessPackageRelates(long resourceId)
			throws Exception {

		WorkspaceResourceBusinessPackageRelateDAOQueryBean queryBean = new WorkspaceResourceBusinessPackageRelateDAOQueryBean();

		queryBean.createCriteria().andWorkspaceResourceIdEqualTo(resourceId);

		// queryBean
		// .setSortProperty(IWorkspaceResourceBusinessPackageRelateDAO.);

		return WorkspaceResourceBusinessPackageRelateBean
				.toArray(this.workspaceResourceBusinessPackageRelateDao.query(queryBean));
	}

	@Override
	protected File doGetWorkspaceResourceFileObject(WorkspaceResourceBean workspaceResourceBean) throws Exception {
		Workspace workspace = this.workspaceDao.findById(workspaceResourceBean.getWorkspaceId());

		File rootFile = getWorkspaceRoot();

		StringBuffer resourcePath = new StringBuffer(workspace.getWorkspacePath());

		if (workspace.getWorkspacePath().endsWith("/") == false) {
			resourcePath.append("/");
		}

		if (workspaceResourceBean.getResourcePath().startsWith("/")) {
			resourcePath.append(workspaceResourceBean.getResourcePath().substring(1));
		} else {
			resourcePath.append(workspaceResourceBean.getResourcePath());
		}

		File resourcePathFile = new File(rootFile, resourcePath.toString());

		return resourcePathFile;
	}

	@Override
	protected WorkspaceConfigBean[] doGetWorkspaceConfigs(long workspaceId, long parentWorkspaceConfigId)
			throws Exception {

		WorkspaceConfigDAOQueryBean queryBean = new WorkspaceConfigDAOQueryBean();

		queryBean.createCriteria().andParentConfigIdEqualTo(parentWorkspaceConfigId);

		return WorkspaceConfigBean.toArray(this.workspaceConfigDao.query(queryBean));
	}

	@Override
	protected WorkspaceConfigPropertyBean[] doGetWorkspaceConfigProperties(long workspaceId, long workspaceConfigId)
			throws Exception {

		WorkspaceConfigPropertyDAOQueryBean queryBean = new WorkspaceConfigPropertyDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId).andWorkspaceConfigIdEqualTo(workspaceConfigId);

		return WorkspaceConfigPropertyBean.toArray(this.workspaceConfigPropertyDao.query(queryBean));
	}

	@Override
	protected void doSetWorkspaceConfigProperties(long workspaceId, long workspaceConfigId,
			WorkspaceConfigPropertyBean[] workspaceConfigPropertyBeans) throws Exception {

		WorkspaceConfigPropertyDAOQueryBean queryBean = new WorkspaceConfigPropertyDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId).andWorkspaceConfigIdEqualTo(workspaceConfigId);

		WorkspaceConfigProperty[] configProperties = new WorkspaceConfigProperty[workspaceConfigPropertyBeans.length];

		for (int i = 0; i < configProperties.length; i++) {
			configProperties[i] = workspaceConfigPropertyBeans[i].convertToWorkspaceConfigProperty();
			configProperties[i].setWorkspaceId(workspaceId);
			configProperties[i].setWorkspaceConfigId(workspaceConfigId);
		}

		this.workspaceConfigPropertyDao.synchronize(configProperties, queryBean);
	}

	@Override
	protected WorkspaceConfigPropertyBean[] doGetWorkspaceConfigPropertiesByConfigItem(long workspaceId,
			String configItem) throws Exception {

		WorkspaceConfigPropertyDAOQueryBean queryBean = new WorkspaceConfigPropertyDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId).andWorkspaceConfigItemEqualTo(configItem);

		return WorkspaceConfigPropertyBean.toArray(this.workspaceConfigPropertyDao.query(queryBean));
	}

	// @Override
	// protected MiddleWareBean[] doGetServersByProjectIds(long[] projectIds,
	// MiddleWareTypeEnum[] serverTypeEnums) throws Exception {
	//
	// MiddleWareDAOQueryBean queryBean = new MiddleWareDAOQueryBean();
	//
	// MiddleWareDAOQueryBean.Criteria criteria = queryBean.createCriteria();
	//
	// if (ArrayUtils.isNotEmpty(serverTypeEnums)) {
	// String[] serverTypes = new String[serverTypeEnums.length];
	//
	// for (int i = 0; i < serverTypes.length; i++) {
	// serverTypes[i] = serverTypeEnums[i].getName();
	// }
	//
	// criteria.andTypeIn(serverTypes);
	// }
	//
	// ProjectServerRelateDAOQueryBean joinQueryBean = new
	// ProjectServerRelateDAOQueryBean();
	//
	// joinQueryBean.createCriteria().andProjectIdIn(projectIds)
	// .andMiddleWareIdEqualToForeignKey(IMiddleWareDAO.Id);
	//
	// queryBean.addInnerJoin(IProjectServerRelateDAO.EntityName,
	// joinQueryBean);
	//
	// return this.middleWareService.queryMiddleWare(queryBean);
	// }

	@Override
	protected void doSetWorkspaceConfigPropertiesByConfigItem(long workspaceId, String configItem,
			WorkspaceConfigPropertyBean[] workspaceConfigPropertyBean) throws Exception {

		WorkspaceConfigDAOQueryBean queryBean = new WorkspaceConfigDAOQueryBean();

		queryBean.createCriteria().andConfigItemEqualTo(configItem);

		WorkspaceConfig[] workspaceConfig = this.workspaceConfigDao.query(queryBean);

		if (workspaceConfig.length > 0) {

			WorkspaceConfigPropertyDAOQueryBean propertyQueryBean = new WorkspaceConfigPropertyDAOQueryBean();

			propertyQueryBean.clear();

			propertyQueryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId)
					.andWorkspaceConfigIdEqualTo(workspaceConfig[0].getId());

			WorkspaceConfigProperty[] configProperties = this.workspaceConfigPropertyDao.query(propertyQueryBean);

			Map<String, WorkspaceConfigProperty> configMap = new HashMap<>();

			for (int i = 0; i < configProperties.length; i++) {
				configMap.put(configProperties[i].getConfigProperty(), configProperties[i]);
			}

			for (int i = 0; i < workspaceConfigPropertyBean.length; i++) {

				WorkspaceConfigPropertyBean configPropertyBean = workspaceConfigPropertyBean[i];
				configPropertyBean.setWorkspaceId(workspaceId);
				configPropertyBean.setWorkspaceConfigId(workspaceConfig[0].getId());

				if (configMap.containsKey(workspaceConfigPropertyBean[i].getConfigProperty())) {
					configPropertyBean.setId(configMap.get(workspaceConfigPropertyBean[i].getConfigProperty()).getId());
					this.workspaceConfigPropertyDao.update(configPropertyBean.convertToWorkspaceConfigProperty());
				} else {
					this.workspaceConfigPropertyDao.insert(configPropertyBean.convertToWorkspaceConfigProperty());
				}

			}

		}

	}

	@Override
	protected File doGetWorkspaceFileObject(WorkspaceBean workspace) throws Exception {

		File rootFile = getWorkspaceRoot();

		StringBuffer resourcePath = new StringBuffer(workspace.getWorkspacePath());

		File resourcePathFile = new File(rootFile, resourcePath.toString());

		return resourcePathFile;
	}

	@Override
	protected File doGetWorkspaceRootFileObject() throws Exception {
		return getWorkspaceRoot();
	}

	@Override
	protected void doDeleteWorkspace(long workspaceId) throws Exception {

		ProjectWorkspaceRelateDAOQueryBean queryBean = new ProjectWorkspaceRelateDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId);

		// this.projectWorkspaceRelateDao.delete(queryBean);

		WorkspaceConfigPropertyDAOQueryBean configPropertyQueryBean = new WorkspaceConfigPropertyDAOQueryBean();

		configPropertyQueryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId);

		this.workspaceConfigPropertyDao.delete(configPropertyQueryBean);

		WorkspaceResourceDAOQueryBean resourceQueryBean = new WorkspaceResourceDAOQueryBean();

		resourceQueryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId);

		deleteWorkspaceResource(resourceQueryBean);

		// WorkspaceBean workspaceBean = getWorkspaceById(workspaceId);

		// File workspaceDir = getWorkspaceFileObject(workspaceBean);

		// FileUtils.deleteQuietly(workspaceDir);

		this.workspaceDao.delete(workspaceId);

	}

	protected void deleteWorkspaceResource(WorkspaceResourceDAOQueryBean queryBean) throws Exception {

		this.workspaceResourceDao.delete(queryBean);

	}

	@Override
	protected void doDeleteWorkspaceResource(long workspaceResourceId) throws Exception {

		WorkspaceResource resource = this.workspaceResourceDao.findById(workspaceResourceId);

		WorkspaceResourceDAOQueryBean resourceQueryBean = new WorkspaceResourceDAOQueryBean();

		resourceQueryBean.createCriteria().andIdEqualTo(workspaceResourceId);

		deleteWorkspaceResource(resourceQueryBean);

		WorkspaceResourceDAOQueryBean queryBean = new WorkspaceResourceDAOQueryBean();

		queryBean.setSortProperty(IWorkspaceResourceDAO.OrderIndex);

		queryBean.createCriteria().andWorkspaceIdEqualTo(resource.getWorkspaceId());

		WorkspaceResource[] resources = this.workspaceResourceDao.query(queryBean);

		int index = 1;

		for (int i = 0; i < resources.length; i++) {
			resources[i].setOrderIndex(index++);
		}

		this.workspaceResourceDao.batchUpdate(resources);

	}

	@Override
	protected void doRemoveWorkspaceResource(long workspaceResourceId) throws Exception {
		WorkspaceResourceDAOQueryBean resourceQueryBean = new WorkspaceResourceDAOQueryBean();

		resourceQueryBean.createCriteria().andIdEqualTo(workspaceResourceId);

		this.workspaceResourceDao.delete(resourceQueryBean);

		this.workspaceResourceDao.delete(resourceQueryBean);

	}

	@Override
	protected void doAddServerToProject(long middleWareServerId, long projectId) throws Exception {

		ProjectServerRelate projectServerRelate = new ProjectServerRelate();

		projectServerRelate.setProjectId(projectId);

		projectServerRelate.setMiddleWareId(middleWareServerId);

		// this.projectServerRelateDao
		// .insert(projectServerRelate);

	}

	@Override
	protected WorkspaceBean[] doQueryWorkspaces(WorkspaceDAOQueryBean queryBean) throws Exception {
		return WorkspaceBean.toArray(workspaceDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceCount(WorkspaceDAOQueryBean queryBean) throws Exception {
		return workspaceDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceResourceBean[] doQueryWorkspaceResources(WorkspaceResourceDAOQueryBean queryBean)
			throws Exception {
		return WorkspaceResourceBean.toArray(workspaceResourceDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceResourceCount(WorkspaceResourceDAOQueryBean queryBean) throws Exception {
		return workspaceResourceDao.queryCount(queryBean);
	}

	@Override
	protected void doSaveWorkspaceScmConfig(WorkspaceScmConfigBean aWorkspaceScmConfig) throws Exception {

		if (aWorkspaceScmConfig.getId() == 0) {
			workspaceScmConfigDao.insert(aWorkspaceScmConfig.convertToWorkspaceScmConfig());
		} else {
			workspaceScmConfigDao.update(aWorkspaceScmConfig.convertToWorkspaceScmConfig());
		}

	}

	@Override
	protected WorkspaceScmConfigBean doGetWorkspaceScmConfig(long workspaceId) throws Exception {

		WorkspaceScmConfigDAOQueryBean queryBean = new WorkspaceScmConfigDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceId);

		WorkspaceScmConfig[] scmConfigs = workspaceScmConfigDao.query(queryBean);

		if (ArrayUtils.isEmpty(scmConfigs)) {
			return null;
		} else {
			return new WorkspaceScmConfigBean(scmConfigs[0]);
		}
	}

	@Override
	protected void doImportWorkspaceScmLogs(long workspaceScmConfigId, WorkspaceScmLogBean[] scmChangeLogBeans)
			throws Exception {

		WorkspaceScmConfig scmConfig = this.workspaceScmConfigDao.findById(workspaceScmConfigId);

		WorkspaceScmLogDAOQueryBean queryBean = new WorkspaceScmLogDAOQueryBean();

		for (int i = 0; i < scmChangeLogBeans.length; i++) {

			queryBean.clear();

			queryBean.createCriteria().andWorkspaceIdEqualTo(scmConfig.getWorkspaceId())
					.andWorkspaceScmConfigIdEqualTo(workspaceScmConfigId)
					.andRevisionEqualTo(scmChangeLogBeans[i].getRevision());

			this.workspaceScmLogDao.deleteWorkspaceScmChangePathLog(queryBean);

			this.workspaceScmLogDao.delete(queryBean);

			addWorkspaceScmLogs(scmConfig.getWorkspaceId(), workspaceScmConfigId, scmChangeLogBeans[i]);

		}

	}

	protected void addWorkspaceScmLogs(

			long workspaceId, long workspaceScmConfigId, WorkspaceScmLogBean workspaceScmLogBean) throws Exception {

		workspaceScmLogBean.setWorkspaceScmConfigId(workspaceScmConfigId);
		workspaceScmLogBean.setWorkspaceId(workspaceId);

		this.workspaceScmLogDao.insert(workspaceScmLogBean.convertToWorkspaceScmLog());

		WorkspaceScmChangePathLogBean[] changePathLogBeans = workspaceScmLogBean.getChangePathLogs();

		for (int j = 0; j < changePathLogBeans.length; j++) {
			WorkspaceScmChangePathLogBean workspaceScmChangePathLogBean = changePathLogBeans[j];
			workspaceScmChangePathLogBean.setWorkspaceId(workspaceId);
			workspaceScmChangePathLogBean.setWorkspaceScmConfigId(workspaceScmConfigId);
			workspaceScmChangePathLogBean.setWorkspaceScmLogId(workspaceScmLogBean.getId());

			this.workspaceScmChangePathLogDao
					.insert(workspaceScmChangePathLogBean.convertToWorkspaceScmChangePathLog());
		}

		WorkspaceScmLogBean[] scmLogBeans = workspaceScmLogBean.getChildLogBeans();

		for (int i = 0; i < scmLogBeans.length; i++) {
			WorkspaceScmLogBean childLogBean = scmLogBeans[i];
			childLogBean.setParentId(workspaceScmLogBean.getId());
			addWorkspaceScmLogs(workspaceId, workspaceScmConfigId, childLogBean);
		}
	}

	@Override
	protected WorkspaceScmLogBean[] doQueryWorkspaceScmLogs(WorkspaceScmLogDAOQueryBean queryBean) throws Exception {
		return WorkspaceScmLogBean.toArray(this.workspaceScmLogDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceScmLogCount(WorkspaceScmLogDAOQueryBean queryBean) throws Exception {
		return this.workspaceScmLogDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceScmChangePathLogBean[] doQueryWorkspaceScmChangePathLogs(
			WorkspaceScmChangePathLogDAOQueryBean queryBean) throws Exception {
		return WorkspaceScmChangePathLogBean.toArray(this.workspaceScmChangePathLogDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceScmChangePathLogCount(WorkspaceScmChangePathLogDAOQueryBean queryBean)
			throws Exception {
		return this.workspaceScmChangePathLogDao.queryCount(queryBean);
	}

	@Override
	protected void doAddNewWorkspaceBuildConfig(WorkspaceBuildConfigBean aWorkspaceBuildConfig) throws Exception {
		this.workspaceBuildConfigDao.insert(aWorkspaceBuildConfig.convertToWorkspaceBuildConfig());
	}

	@Override
	protected WorkspaceBuildConfigBean doGetWorkspaceBuildConfigById(long aWorkspaceBuildConfigId) throws Exception {

		WorkspaceBuildConfig buildConfig = this.workspaceBuildConfigDao.findById(aWorkspaceBuildConfigId);

		return buildConfig != null ? new WorkspaceBuildConfigBean(buildConfig) : null;
	}

	@Override
	protected void doModifyWorkspaceBuildConfig(WorkspaceBuildConfigBean aWorkspaceBuildConfig) throws Exception {

		this.workspaceBuildConfigDao.update(aWorkspaceBuildConfig.convertToWorkspaceBuildConfig());

	}

	@Override
	protected void doDeleteWorkspaceBuildConfigs(long[] aWorkspaceBuildConfigIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceBuildConfigIds)) {
			return;
		}

		WorkspaceBuildConfigDAOQueryBean queryBean = new WorkspaceBuildConfigDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceBuildConfigIds);

		this.workspaceBuildConfigDao.delete(queryBean);
	}

	@Override
	protected WorkspaceBuildConfigBean[] doQueryWorkspaceBuildConfigs(WorkspaceBuildConfigDAOQueryBean queryBean)
			throws Exception {
		return WorkspaceBuildConfigBean.toArray(this.workspaceBuildConfigDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceBuildConfigCount(WorkspaceBuildConfigDAOQueryBean queryBean) throws Exception {
		return this.workspaceBuildConfigDao.queryCount(queryBean);
	}

	@Override
	protected void doAddNewWorkspaceBuildLog(WorkspaceBuildLogBean aWorkspaceBuildLog) throws Exception {
		this.workspaceBuildLogDao.insert(aWorkspaceBuildLog.convertToWorkspaceBuildLog());
	}

	@Override
	protected void doModifyWorkspaceBuildLog(WorkspaceBuildLogBean aWorkspaceBuildLog) throws Exception {
		this.workspaceBuildLogDao.update(aWorkspaceBuildLog.convertToWorkspaceBuildLog());

	}

	@Override
	protected WorkspaceBuildLogBean doGetWorkspaceBuildLogById(long aWorkspaceBuildLogId) throws Exception {
		WorkspaceBuildLog buildLog = this.workspaceBuildLogDao.findById(aWorkspaceBuildLogId);
		return buildLog != null ? new WorkspaceBuildLogBean(buildLog) : null;
	}

	@Override
	protected void doDeleteWorkspaceBuildLogs(long[] aWorkspaceBuildLogIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceBuildLogIds)) {
			return;
		}

		WorkspaceBuildLogDAOQueryBean queryBean = new WorkspaceBuildLogDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceBuildLogIds);

		this.workspaceBuildLogDao.delete(queryBean);

	}

	@Override
	protected WorkspaceBuildLogBean[] doQueryWorkspaceBuildLogs(WorkspaceBuildLogDAOQueryBean queryBean)
			throws Exception {
		return WorkspaceBuildLogBean.toArray(this.workspaceBuildLogDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceBuildLogCount(WorkspaceBuildLogDAOQueryBean queryBean) throws Exception {
		return this.workspaceBuildLogDao.queryCount(queryBean);
	}

	@Override
	protected void doAddWorkspaceMember(WorkspaceMemberBean aWorkspaceMember) throws Exception {
		this.workspaceMemberDao.insert(aWorkspaceMember.convertToWorkspaceMember());

	}

	@Override
	protected void doModifyWorkspaceMember(WorkspaceMemberBean aWorkspaceMember) throws Exception {
		this.workspaceMemberDao.update(aWorkspaceMember.convertToWorkspaceMember());
	}

	@Override
	protected WorkspaceMemberBean doGetWorkspaceMemberById(long aWorkspaceMemberId) throws Exception {
		WorkspaceMember member = this.workspaceMemberDao.findById(aWorkspaceMemberId);
		return member != null ? new WorkspaceMemberBean(member) : null;
	}

	@Override
	protected void doDeleteWorkspaceMembers(long[] aWorkspaceMemberIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceMemberIds)) {
			return;
		}

		WorkspaceMemberDAOQueryBean queryBean = new WorkspaceMemberDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceMemberIds);

		this.workspaceMemberDao.delete(queryBean);

	}

	@Override
	protected WorkspaceMemberBean[] doQueryWorkspaceMembers(WorkspaceMemberDAOQueryBean queryBean) throws Exception {
		return WorkspaceMemberBean.toArray(this.workspaceMemberDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceMemberCount(WorkspaceMemberDAOQueryBean queryBean) throws Exception {
		return this.workspaceMemberDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceConfigBean doGetWorkspaceConfigById(long aWorkspaceConfigId) throws Exception {

		WorkspaceConfig config = this.workspaceConfigDao.findById(aWorkspaceConfigId);

		return config != null ? new WorkspaceConfigBean(config) : null;
	}

	@Override
	protected void doAddNewWorkspaceServer(WorkspaceServerBean aWorkspaceServer) throws Exception {
		this.workspaceServerDao.insert(aWorkspaceServer.convertToWorkspaceServer());

	}

	@Override
	protected WorkspaceServerBean doGetWorkspaceServerById(long aWorkspaceServerId) throws Exception {
		WorkspaceServer workspaceServer = this.workspaceServerDao.findById(aWorkspaceServerId);
		return workspaceServer != null ? new WorkspaceServerBean(workspaceServer) : null;
	}

	@Override
	protected void doModifyWorkspaceServer(WorkspaceServerBean aWorkspaceServer) throws Exception {

		this.workspaceServerDao.update(aWorkspaceServer.convertToWorkspaceServer());

	}

	@Override
	protected WorkspaceServerBean[] doQueryWorkspaceServers(WorkspaceServerDAOQueryBean queryBean) throws Exception {
		return WorkspaceServerBean.toArray(this.workspaceServerDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceServerCount(WorkspaceServerDAOQueryBean queryBean) throws Exception {
		return this.workspaceServerDao.queryCount(queryBean);
	}

	@Override
	protected void doDeleteWorkspaceServers(long[] aWorkspaceServerIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceServerIds)) {
			return;
		}

		WorkspaceServerDAOQueryBean queryBean = new WorkspaceServerDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceServerIds);

		this.workspaceServerDao.delete(queryBean);

	}

	@Override
	protected void doAddNewWorkspaceFileTypeConfig(WorkspaceFileTypeConfigBean aWorkspaceFileTypeConfig)
			throws Exception {

		this.workspaceFileTypeConfigDao.insert(aWorkspaceFileTypeConfig.convertToWorkspaceFileTypeConfig());

	}

	@Override
	protected void doModifyWorkspaceFileTypeConfig(WorkspaceFileTypeConfigBean aWorkspaceFileTypeConfig)
			throws Exception {

		this.workspaceFileTypeConfigDao.update(aWorkspaceFileTypeConfig.convertToWorkspaceFileTypeConfig());

	}

	@Override
	protected WorkspaceFileTypeConfigBean doGetWorkspaceFileTypeConfigById(long aWorkspaceFileTypeConfigId)
			throws Exception {

		WorkspaceFileTypeConfig fileTypeConfig = this.workspaceFileTypeConfigDao.findById(aWorkspaceFileTypeConfigId);

		return fileTypeConfig != null ? new WorkspaceFileTypeConfigBean(fileTypeConfig) : null;
	}

	@Override
	protected void doDeleteWorkspaceFileTypeConfigs(long[] aWorkspaceFileTypeConfigIds) throws Exception {

		WorkspaceFileTypeConfigDAOQueryBean queryBean = new WorkspaceFileTypeConfigDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceFileTypeConfigIds);

		this.workspaceFileTypeConfigDao.delete(queryBean);

	}

	@Override
	protected WorkspaceFileTypeConfigBean[] doQueryWorkspaceFileTypeConfigs(
			WorkspaceFileTypeConfigDAOQueryBean queryBean) throws Exception {
		return WorkspaceFileTypeConfigBean.toArray(this.workspaceFileTypeConfigDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceFileTypeConfigCount(WorkspaceFileTypeConfigDAOQueryBean queryBean) throws Exception {
		return this.workspaceFileTypeConfigDao.queryCount(queryBean);
	}

	@Override
	protected void doAddNewWorkspaceCompiler(WorkspaceCompilerBean aWorkspaceCompiler) throws Exception {

		if (BooleanUtils.toBoolean(aWorkspaceCompiler.getDefaultCompiler())) {

			WorkspaceCompiler compiler = new WorkspaceCompiler();

			compiler.setDefaultCompiler("false");

			this.workspaceCompilerDao.update(compiler, new WorkspaceCompilerDAOQueryBean());
		}

		this.workspaceCompilerDao.insert(aWorkspaceCompiler.convertToWorkspaceCompiler());
	}

	@Override
	protected void doModifyWorkspaceCompiler(WorkspaceCompilerBean aWorkspaceCompiler) throws Exception {

		if (BooleanUtils.toBoolean(aWorkspaceCompiler.getDefaultCompiler())) {

			WorkspaceCompiler compiler = new WorkspaceCompiler();

			compiler.setDefaultCompiler("false");

			this.workspaceCompilerDao.update(compiler, new WorkspaceCompilerDAOQueryBean());
		}

		this.workspaceCompilerDao.update(aWorkspaceCompiler.convertToWorkspaceCompiler());
	}

	@Override
	protected WorkspaceCompilerBean doGetWorkspaceCompilerById(long aWorkspaceCompilerId) throws Exception {

		WorkspaceCompiler workspaceCompiler = this.workspaceCompilerDao.findById(aWorkspaceCompilerId);

		return workspaceCompiler != null ? new WorkspaceCompilerBean(workspaceCompiler) : null;
	}

	@Override
	protected void doDeleteWorkspaceCompilers(long[] aWorkspaceCompilerIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceCompilerIds)) {
			return;
		}

		WorkspaceCompilerDAOQueryBean queryBean = new WorkspaceCompilerDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceCompilerIds);

		this.workspaceCompilerDao.delete(queryBean);

	}

	@Override
	protected WorkspaceCompilerBean[] doQueryWorkspaceCompilers(WorkspaceCompilerDAOQueryBean queryBean)
			throws Exception {
		return WorkspaceCompilerBean.toArray(this.workspaceCompilerDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceCompilerCount(WorkspaceCompilerDAOQueryBean queryBean) throws Exception {
		return this.workspaceCompilerDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceConfigPropertyBean[] doQueryWorkspaceConfigProperties(
			WorkspaceConfigPropertyDAOQueryBean queryBean) throws Exception {

		return WorkspaceConfigPropertyBean.toArray(this.workspaceConfigPropertyDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceConfigPropertyCount(WorkspaceConfigPropertyDAOQueryBean queryBean) throws Exception {
		return this.workspaceConfigPropertyDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceResourceBusinessPackageRelateBean[] doQueryWorkspaceResourceBusinessPackages(
			WorkspaceResourceBusinessPackageRelateDAOQueryBean queryBean) throws Exception {

		return WorkspaceResourceBusinessPackageRelateBean
				.toArray(this.workspaceResourceBusinessPackageRelateDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceResourceBusinessPackageCount(
			WorkspaceResourceBusinessPackageRelateDAOQueryBean queryBean) throws Exception {
		return this.workspaceResourceBusinessPackageRelateDao.queryCount(queryBean);
	}

	@Override
	protected void doModifyWorkspaceResourceBusinessPackageRelates(
			WorkspaceResourceBusinessPackageRelateBean[] aworkspaceResourceBusinessPackageRelates) throws Exception {

		WorkspaceResourceBusinessPackageRelate[] businessPackageRelates = new WorkspaceResourceBusinessPackageRelate[aworkspaceResourceBusinessPackageRelates.length];

		for (int i = 0; i < businessPackageRelates.length; i++) {
			businessPackageRelates[i] = aworkspaceResourceBusinessPackageRelates[i].convertTo();
		}

		this.workspaceResourceBusinessPackageRelateDao.batchUpdate(businessPackageRelates);
	}

	@Override
	protected WorkspaceProductDatabaseRelateBean doAddNewWorkspaceProductDatabaseRelate(
			WorkspaceProductDatabaseRelateBean aWorkspaceProductDatabaseRelate) throws Exception {

		this.workspaceProductDatabaseRelateDao.insert(aWorkspaceProductDatabaseRelate.convertTo());

		return aWorkspaceProductDatabaseRelate;
	}

	@Override
	protected WorkspaceProductDatabaseRelateBean doModifyWorkspaceProductDatabaseRelate(
			WorkspaceProductDatabaseRelateBean aWorkspaceProductDatabaseRelate) throws Exception {

		this.workspaceProductDatabaseRelateDao.update(aWorkspaceProductDatabaseRelate.convertTo());

		return aWorkspaceProductDatabaseRelate;
	}

	@Override
	protected void doDeleteWorkspaceProductDatabaseRelates(long[] aWorkspaceProductDatabaseRelateIds) throws Exception {

		if (ArrayUtils.isNotEmpty(aWorkspaceProductDatabaseRelateIds)) {

			WorkspaceProductDatabaseRelateDAOQueryBean queryBean = new WorkspaceProductDatabaseRelateDAOQueryBean();

			queryBean.createCriteria().andIdIn(aWorkspaceProductDatabaseRelateIds);

			this.workspaceProductDatabaseRelateDao.deleteWorkspaceProductDatabaseModelRelate(queryBean);

			this.workspaceProductDatabaseRelateDao.delete(queryBean);

		}

	}

	@Override
	protected WorkspaceProductDatabaseRelateBean doGetWorkspaceProductDatabaseRelateById(
			long aWorkspaceProductDatabaseRelateId) throws Exception {

		WorkspaceProductDatabaseRelate databaseRelate = this.workspaceProductDatabaseRelateDao
				.findById(aWorkspaceProductDatabaseRelateId);

		return databaseRelate != null ? new WorkspaceProductDatabaseRelateBean(databaseRelate) : null;
	}

	@Override
	protected WorkspaceProductDatabaseRelateBean[] doQueryWorkspaceProductDatabaseRelates(
			WorkspaceProductDatabaseRelateDAOQueryBean queryBean) throws Exception {
		return WorkspaceProductDatabaseRelateBean.toArray(this.workspaceProductDatabaseRelateDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceProductDatabaseRelateCount(WorkspaceProductDatabaseRelateDAOQueryBean queryBean)
			throws Exception {
		return this.workspaceProductDatabaseRelateDao.queryCount(queryBean);
	}

	@Override
	protected WorkspaceProductDatabaseModelRelateBean doAddNewWorkspaceProductDatabaseModelRelate(
			WorkspaceProductDatabaseModelRelateBean aWorkspaceProductDatabaseModelRelate) throws Exception {

		this.workspaceProductDatabaseModelRelateDao.insert(aWorkspaceProductDatabaseModelRelate.convertTo());

		return aWorkspaceProductDatabaseModelRelate;
	}

	@Override
	protected WorkspaceProductDatabaseModelRelateBean doModifyWorkspaceProductDatabaseModelRelate(
			WorkspaceProductDatabaseModelRelateBean aWorkspaceProductDatabaseModelRelate) throws Exception {

		this.workspaceProductDatabaseModelRelateDao.update(aWorkspaceProductDatabaseModelRelate.convertTo());

		return aWorkspaceProductDatabaseModelRelate;
	}

	@Override
	protected void doDeleteWorkspaceProductDatabaseModelRelates(long[] aWorkspaceProductDatabaseModelRelateIds)
			throws Exception {

		if (ArrayUtils.isNotEmpty(aWorkspaceProductDatabaseModelRelateIds)) {

			WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean = new WorkspaceProductDatabaseModelRelateDAOQueryBean();

			queryBean.createCriteria().andIdIn(aWorkspaceProductDatabaseModelRelateIds);

			this.workspaceProductDatabaseModelRelateDao.delete(queryBean);

		}

	}

	@Override
	protected WorkspaceProductDatabaseModelRelateBean doGetWorkspaceProductDatabaseModelRelateById(
			long aWorkspaceProductDatabaseModelRelateId) throws Exception {

		WorkspaceProductDatabaseModelRelate databaseModelRelate = this.workspaceProductDatabaseModelRelateDao
				.findById(aWorkspaceProductDatabaseModelRelateId);

		return databaseModelRelate != null ? new WorkspaceProductDatabaseModelRelateBean(databaseModelRelate) : null;
	}

	@Override
	protected WorkspaceProductDatabaseModelRelateBean[] doQueryWorkspaceProductDatabaseModelRelates(
			WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean) throws Exception {

		return WorkspaceProductDatabaseModelRelateBean
				.toArray(this.workspaceProductDatabaseModelRelateDao.query(queryBean));

	}

	@Override
	protected int doQueryWorkspaceProductDatabaseModelRelateCount(
			WorkspaceProductDatabaseModelRelateDAOQueryBean queryBean) throws Exception {

		return this.workspaceProductDatabaseModelRelateDao.queryCount(queryBean);

	}

	@Override
	protected void doAddNewWorkspaceResourceUIPageRelate(
			WorkspaceResourceUIPageRelateBean aWorkspaceResourceUIPageRelate) throws Exception {

		this.workspaceResourceUIPageRelateDao.insert(aWorkspaceResourceUIPageRelate.convertTo());

	}

	@Override
	protected void doModifyWorkspaceResourceUIPageRelate(
			WorkspaceResourceUIPageRelateBean aWorkspaceResourceUIPageRelate) throws Exception {

		this.workspaceResourceUIPageRelateDao.update(aWorkspaceResourceUIPageRelate.convertTo());

	}

	@Override
	protected void doDeleteWorkspaceResourceUIPageRelates(long[] aWorkspaceResourceUIPageRelateIds) throws Exception {

		if (ArrayUtils.isEmpty(aWorkspaceResourceUIPageRelateIds)) {
			return;
		}

		WorkspaceResourceUIPageRelateDAOQueryBean queryBean = new WorkspaceResourceUIPageRelateDAOQueryBean();

		queryBean.createCriteria().andIdIn(aWorkspaceResourceUIPageRelateIds);

		this.workspaceResourceUIPageRelateDao.delete(queryBean);

	}

	@Override
	protected WorkspaceResourceUIPageRelateBean doGetWorkspaceResourceUIPageRelateById(
			long aWorkspaceResourceUIPageRelateId) throws Exception {

		WorkspaceResourceUIPageRelate uiPageRelate = this.workspaceResourceUIPageRelateDao
				.findById(aWorkspaceResourceUIPageRelateId);

		return uiPageRelate != null ? new WorkspaceResourceUIPageRelateBean(uiPageRelate) : null;
	}

	@Override
	protected WorkspaceResourceUIPageRelateBean[] doQueryWorkspaceResourceUIPageRelates(
			WorkspaceResourceUIPageRelateDAOQueryBean queryBean) throws Exception {
		return WorkspaceResourceUIPageRelateBean.toArray(this.workspaceResourceUIPageRelateDao.query(queryBean));
	}

	@Override
	protected int doQueryWorkspaceResourceUIPageRelateCount(WorkspaceResourceUIPageRelateDAOQueryBean queryBean)
			throws Exception {
		return this.workspaceResourceUIPageRelateDao.queryCount(queryBean);
	}

	@Override
	protected void doAddWorkspaceResourceUIPages(long workspaceResourceId, long[] uiPageIds) throws Exception {

		if (ArrayUtils.isEmpty(uiPageIds)) {
			return;
		}

		WorkspaceResourceUIPageRelateDAOQueryBean queryBean = new WorkspaceResourceUIPageRelateDAOQueryBean();

		queryBean.createCriteria().andWorkspaceIdEqualTo(workspaceResourceId).andUiPageIdIn(uiPageIds);

		WorkspaceResourceUIPageRelate[] uiPageRelates = this.workspaceResourceUIPageRelateDao.query(queryBean);

		List<Long> uiPageIdList = new ArrayList<Long>();

		for (int i = 0; i < uiPageRelates.length; i++) {
			uiPageIdList.add(uiPageRelates[i].getUiPageId());
		}

		WorkspaceResourceUIPageRelate[] pageRelates = new WorkspaceResourceUIPageRelate[uiPageIds.length
				- uiPageRelates.length];

		for (int i = 0; i < pageRelates.length; i++) {
			if (uiPageIdList.contains(uiPageIds[i])) {
				continue;
			}
			pageRelates[i] = new WorkspaceResourceUIPageRelate();
			pageRelates[i].setWorkspaceResourceId(workspaceResourceId);
			pageRelates[i].setUiPageId(uiPageIds[i]);
		}

		this.workspaceResourceUIPageRelateDao.batchInsert(pageRelates);

	}

}